import {routerRedux} from 'dva/router';
import api from '../api';
import {message} from 'antd'

export default {
  namespace : 'portfolioPerformance',
  state : {
    lPortId: null,// 产品组合ID 默认值 产品ID排序第一的产品 || query中的 lPortId
    cBenchCode: null,// 业绩基准ID 默认值 沪深300 || query中的 cBenchCode
    timeRange: '2', // 净值走势时间段 默认值 最近一月
    latestNetValue: null, // 最新净值 默认值 null
    realTimeNetValue: null, // 实时净值 默认值 	null
    unitNetValueTrend: [], // 单位净值走势 默认值 []
    totalNetValueTrend: [], // 累计净值走势 默认值 []
    netValuePerformance: [],// 净值表现 默认值 []
    monthlyReturn: [],// 月度回报数据 默认值 []
  },
  subscriptions : {
    setup({dispatch, history}) {
      history.listen(({pathname, query}) => {
        // 没有参数时，向后端请求数据，并且改变query
        if (pathname === '/customerService/combinatorialAnalysis/portfolioPerformance') {
          dispatch({type: 'init', payload: query})
        }
      })
    }
  },
  effects : {
    * init({
				payload
			}, {call, put,select}) {
        // 初始化
				const state=yield select(state => state.portfolioPerformance)
				// 看本次的参数和上次的参数是否一致：
				// 一致的话，不请求，不一致的话，正常请求
				// 定义一个对象用于定义参数
				const newObj={}
				for(var key in payload){
					newObj[key]=state[key]
				}
				if(!Object.getOwnPropertyNames(payload).length||JSON.stringify(payload)!=JSON.stringify(newObj)){
          yield put({
						type:'setState',
						payload,
					})
				}
    },

    * getLatestNetValue({
      payload
    }, {call, put, select}) {
      // 获取最新净值数据
      const query={}
      payload.lPortId&&(query.lPortId=payload.lPortId)
      payload.cBenchCode&&(query.cBenchCode=payload.cBenchCode)

      const res = yield call(api.netValue.getLatestNetValue, query)
      if (res.code === 0) {
        const latestNetValue = res.data
        yield put({type: 'setState', payload: {
          latestNetValue,
          }})
      } else {
        message.info('获取最新净值数据失败！')
      }
    },
    * getRealTimeNetValue({
      payload
    }, {call, put, select}) {
      // 获取实时净值数据
      const query={}
      payload.lPortId&&(query.lPortId=payload.lPortId)
      payload.cBenchCode&&(query.cBenchCode=payload.cBenchCode)

      const res = yield call(api.netValue.getRealTimeNetValue, query)
      if (res.code === 0) {
        const realTimeNetValue = res.data
        yield put({type: 'setState', payload: {
          realTimeNetValue,
          }})
      } else {
        message.info('获取实时净值数据失败！')
      }
    },

    * getNetValueTrend({
    payload
  }, {call, put, select}) {
    // 获取净值走势数据
    const query={}
    payload.lPortId&&(query.lPortId=payload.lPortId)
    payload.cBenchCode&&(query.cBenchCode=payload.cBenchCode)
    payload.timeRange&&(query.timeRange=payload.timeRange)
    

    const res = yield call(api.netValue.getNetValueTrend, query)
    if (res.code === 0) {
      const netValueTrend = res.data
      yield put({type: 'setState', payload: {
        netValueTrend,
        }})
    } else {
      message.info('获取净值走势数据失败！')
    }
  },

  * getNetValuePerformance({
  payload
}, {call, put, select}) {
  // 获取净值表现数据
  const query={}
  payload.lPortId&&(query.lPortId=payload.lPortId)
  payload.cBenchCode&&(query.cBenchCode=payload.cBenchCode)

  const res = yield call(api.netValue.getNetValuePerformance, query)
  if (res.code === 0) {
    const netValuePerformance = res.data
    yield put({type: 'setState', payload: {
      netValuePerformance,
      }})
  } else {
    message.info('获取净值表现数据失败！')
  }
},

* getMonthlyReturn({
  payload
}, {call, put, select}) {
  // 获取月度回报数据
  const query={}
      payload.lPortId&&(query.lPortId=payload.lPortId)
      payload.cBenchCode&&(query.cBenchCode=payload.cBenchCode)

  const res = yield call(api.netValue.getMonthlyReturn, query)
  if (res.code === 0) {
    const monthlyReturn = res.data
    yield put({type: 'setState', payload: {
      monthlyReturn,
      }})
  } else {
    message.info('获取月度回报数据失败！')
  }
},
  
   
  },
  reducers : {
    setState(state, {payload}) {
      return {
        ...state,
        ...payload
      }
    }
  }
}
